﻿using MyCourseManageProject.Bll;
using MyCourseManageProject.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyCourseManageProject.UI
{
    public partial class FrmCourseManage : Form
    {
        private CourseCategroysBll cbll = new CourseCategroysBll();
        private CoursesBll cobll = new CoursesBll();

        //创建数据集合,方便存储查询属性 同时把属性赋值给修改的页面
        private List<Courses> clist = null;
        public FrmCourseManage()
        {
            InitializeComponent();
            List<CourseCategroys> clist = cbll.GetCourseCategories();
            //第一行插入空白
            clist.Insert(0, new CourseCategroys { CategroyId = -1, CategroyName = "==请选择==" });
            //动态填充课程分类下拉框
            this.cbbCategory.DataSource = clist;
            this.cbbCategory.DisplayMember = "CategroyName";
            this.cbbCategory.ValueMember = "CategroyId";
            this.cbbCategory.SelectedIndex = -1;//不绑定

            //其他初始化内容
            this.paneIModify.Visible = false;//隐藏修改面板
            this.btnModify.Enabled = this.btnDelete.Enabled = false;//禁用修改和删除按钮
            this.dvgCourseList.AutoGenerateColumns = false;//自动生成列关闭


            //给需要修改的课程对象设置下拉框数据源
            //this.cbbCategory1.DataSource = clist;//直接引用的方式,会导致两个下拉框同步变换
            this.cbbCategory1.DataSource = new List<CourseCategroys>(clist);  //解决以上一行问题的方案(将前面的集合对象重新复制后再做数据源)
            this.cbbCategory1.DisplayMember = "CategroyName";
            this.cbbCategory1.ValueMember = "CategroyId";
            this.cbbCategory1.SelectedIndex = -1;//不绑定

        }

        //提交查询
        private void btnCheck_Click(object sender, EventArgs e)
        {
            //1条件验证
            if ((this.cbbCategory.SelectedIndex == -1 || this.cbbCategory.SelectedIndex == 0) && (this.txtCourseName.Text.Trim().Length == 0))
            {
                MessageBox.Show("请选择一个查询条件", "提示信息");
                return;
            }
            //2提交查询
            int categroyId = this.cbbCategory.SelectedIndex == -1 ? -1 : Convert.ToInt32(this.cbbCategory.SelectedValue);
            string courseName = this.txtCourseName.Text.Trim();
            clist = cobll.GetAll(categroyId, courseName);
            this.dvgCourseList.DataSource = clist;

            this.lblCount.Text = this.dvgCourseList.RowCount.ToString();
            if (this.dvgCourseList.RowCount == 0)
            {
                this.btnModify.Enabled = this.btnDelete.Enabled = false;//禁用修改和删除按钮
                MessageBox.Show("没有找到查询结果", "提示信息");
            }
            else
            {
                this.btnModify.Enabled = this.btnDelete.Enabled = true;//启用修改和删除按钮
            }


        }
        //修改课程
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            //1验证条件
            if (this.dvgCourseList.CurrentRow == null)
            {
                MessageBox.Show("请选择需要修改的数据","提示信息");
                return;
            }

            //2获取当前选中行课程Id(唯一标识属性)
            int courseId = Convert.ToInt32(this.dvgCourseList.CurrentRow.Cells["CourseId"].Value);
            //3依据获取的Id从集合中查询课程对象
            Courses currentCourse = null;
            //foreach (var item in clist)
            //{
            //    if (item.CategoryId.Equals(courseId))
            //    {
            //        currentCourse = item;
            //        break;
            //    }
            //}
            //高级写法
            // currentCourse = clist.Where(c => c.CategoryId.Equals(courseId)).FirstOrDefault();
               currentCourse = (from c in clist where c.CourseId == courseId select c).FirstOrDefault();

            //4显示要修改的课程对象
            this.txtCourseName1.Text = currentCourse.CourseName;
            this.txtCourseContent1.Text = currentCourse.CourseCountent;
            this.txtCredit1.Text = currentCourse.Credit.ToString();
            this.txtClassHour1.Text = currentCourse.ClassHour.ToString();
            this.lbltCourseId.Text = currentCourse.CourseId.ToString();//为提交修改使用
            this.paneIModify.Visible = true;//显示修改面板
            this.cbbCategory1.SelectedValue = currentCourse.CategoryId;

        }

        //提交修改信息
        private void btnSaveDB_Click(object sender, EventArgs e)
        {
            //1对要修改的信息校验  Credit:学分不能超过30分


            //2封装对象
            Courses cs = new Courses()
            {
                CourseId = Convert.ToInt32(this.lbltCourseId.Text),
                CourseName = this.txtCourseName1.Text.Trim(),
                CourseCountent = this.txtCourseContent1.Text.Trim(),
                ClassHour = Convert.ToInt32(this.txtClassHour1.Text.Trim()),
                Credit = Convert.ToInt32(this.txtCredit1.Text.Trim()),
                CategoryId = Convert.ToInt32(this.cbbCategory.SelectedValue),
                TeacherId = Program.currentTeacher.TeacherId,
                TeacherName = this.cbbCategory1.Text.Trim()
            };
            //3调用后台提交修改(没有考虑异常)
            string msg;
            bool Isok = cobll.UpdateCourses(cs, out msg);

            this.paneIModify.Visible = false;//隐藏修改面板
            MessageBox.Show(msg);

            //4同步显示要修改的信息(改进用户体验) (从缓存中修改对象)
            Courses currentCourse = clist.Where(c => c.CourseId.Equals(cs.CourseId)).FirstOrDefault();
            currentCourse.CourseName = cs.CourseName;
            currentCourse.CategoryName = cs.CategoryName;
            currentCourse.CategoryId = cs.CategoryId;
            currentCourse.CourseCountent = cs.CourseCountent;
            currentCourse.Credit = cs.Credit;
            currentCourse.ClassHour = cs.ClassHour;
            //同步刷新修改结果
            this.dvgCourseList.Refresh();

        }
        //关闭修改Panal
        private void btnCloseP_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //删除课程
        private void btnDelect_Click(object sender, EventArgs e)
        {
            //1验证条件
            if (this.dvgCourseList.CurrentRow == null)
            {
                MessageBox.Show("请选择需要修改的数据");
                return;
            }

            //2获取当前选中行课程Id(唯一标识属性)
            int courseId = Convert.ToInt32(this.dvgCourseList.CurrentRow.Cells["CourseId"].Value);
            //删除前的确认
            DialogResult result = MessageBox.Show("您却要删除编号为" + courseId + "的课程吗?",
            "删除确认",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
            if (result == DialogResult.Cancel) return;
          
            //3从后台删除
            string msg;
            bool isOk = cobll.DelectCourses(new Courses { CourseId=courseId}, out msg);
               //4从集合中删除对象
            clist.Remove(clist.Where(c=>c.CourseId==courseId).First());
           //5同步刷新显示
            this.dvgCourseList.DataSource = null;
            this.dvgCourseList.DataSource = clist;
            this.lblCount.Text = clist.Count.ToString();
            MessageBox.Show(msg);
           

        }
        //关闭窗口
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }



    }
}
